package chapter8;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/3/8 15:33
 */
public class Shared {
    private static long counter = 0;
    private final long id = counter++;
    private int refcount = 0;

    public Shared() {
        System.out.println("creating " + this);
    }

    public void addRef() {
        refcount++;
    }

    protected void dispose() {
        if (--refcount == 0) {
            System.out.println("Disposing " + this);
        }
    }

    @Override
    public String toString() {
        return "Shared " + id;
    }
}
